<?php

namespace app\home\admin;
use app\admin\controller\Admin;
use app\home\model\User;
use function foo\func;
use think\Db;
use think\Loader;
use think\Request;

class Taps extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function taplist(Request $request)
    {
//        根据角色来获取对应的用户列表
        $data = $request->param();
//        $validate = validate('Taps');
//        $res = $validate->scene('list')->check($data);
//        if (!isset($data['role'])){
//            $data['role'] = 1;
//        }
//        if (!$res){
//            return $this->error($validate->getError());
//        }

        //如果设置了删选是否审核
//        if (isset($data['ischeck']) && $data['ischeck'] != 3){
//            if ($data['ischeck'] == 1){
//                $where['ischeck'] = 0;
//            }else{
//                $where['ischeck'] = 1;
//            }
//
//        }
        //是否设置了关键字
        if (isset($data['keyword']) && !empty($data['keyword'])){
            $where['name'] = ['like',"%{$data['keyword']}%"];
        }
        //设置筛选是否禁用用户查询
//        if (isset($data['status']) && $data['status'] != 3){
//            $where['status'] = $data['status'];
//        }
        $where['type'] = $data['type'];
//        if (!empty($data['createtime'])){
//            $time = explode('~',$data['createtime']);
//            $start = strtotime($time[0]);
//            $end = strtotime($time[1]);
//            $where['createtime'] = ['between',[$start,$end]];
//        }
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $listrow = $data['listrow'] :  $listrow = 10;
        $res = db('home_taps')->where($where)->paginate(10)->each(function ($item){
            $item['create_time'] = date("Y-m-d H:i:s",$item['create_time']);
            return $item;
        });
        if ($res){
            $this->assign('taplist',$res);
            $this->assign('type',$data['type']);
        }else{
            return $this->success('无数据');
        }
        return $this->fetch('index');


    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            $insert['name'] = $data['name'];
            $insert['type'] = $data['type'];
            if ($insert['type'] != 1){
               $insert['price'] = $data['price1']."-".$data['price2'];
            }
            $insert['create_time'] = time();
            $res = \db('home_taps')->insert($insert);
            if ($res){
                return $this->success('新增成功');
            }else{
                return $this->error('添加失败');
            }
        }else{
            $type = $data['type'];
            $this->assign('type',$type);
            return $this->fetch();
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            $insert['id'] = $data['id'];
            $insert['name'] = $data['name'];
            $insert['type'] = $data['type'];
            if ($insert['type'] !== 1){
                $insert['price'] = $data['price1']."-".$data['price2'];
            }
            $res = \db('home_taps')->update($insert);
            if ($res){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }
        $id = $data['id'];
        $info = \db('home_taps')->where('id',$id)->find();
        if ($info){
            if (!empty($info['price'])){
                $array = explode('-',$info['price']);
                $info['price1'] = $array[0];
                $info['price2'] = $array[1];
            }
            $this->assign('list',$info);
            $this->assign('type',$data['type']);
            return $this->fetch('add');
        }else{
            return $this->error('无数据');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deltaps(Request $request)
    {
        $data = $request->param();
        if (isset($data['ids'])){
            $ids = $data['ids'];
            $where['id'] = ['in',$ids];
        }else{
            $where['id'] = $data['id'];
        }

        $res = \db('home_taps')->where($where)->delete();
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function changestatus(Request $request)
    {
        $data = $request->param();
        if (!empty($data['id'])){
            $id = $data['id'];
        }else{
            return $this->error('参数缺失');
        }
        if ($data['val'] == 0){
            $update['status'] = 2;
        }elseif($data['val'] == 1){
            $update['status'] = 1;
        }
        $result = Db::table('rd_home_taps')->where('id',$id)->update($update);
        if ($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
}
